
const baseUrl = "https://mall.moujiangyong.com"


/**
 * 无参GET请求带加载
 * @param {Object} url
 */
export function get_(url) {
	uni.showLoading({
		title: "加载中"
	})
	let token = uni.getStorageSync("token")
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			header: {
				'token': token
			},
			success: (res) => {
				uni.hideLoading()
				if (res.data.code == 2000) {
					resolve(res.data)
				} else if (res.data.code == 3000) {
					uni.navigateTo({
						url: "/pages/login/index"
					})
				} else {
					uni.showToast({
						icon: "none",
						title: res.data.message
					})
				}

			},
			fail: () => {
				uni.hideLoading()
				uni.showToast({
					icon: "none",
					title: "网络错误"
				})
				reject("error")
			}
		})
	})
}

/**
 * 无参GET请求
 * @param {Object} url
 */
export function get(url) {

	let token = uni.getStorageSync("token")
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			header: {
				'token': token
			},
			success: (res) => {

				if (res.data.code == 2000) {
					resolve(res.data)
				} else if (res.data.code == 3000) {
					uni.navigateTo({
						url: "/pages/login/index"
					})
				} else {
					uni.showToast({
						icon: "none",
						title: res.data.message
					})
				}

			},
			fail: () => {

				reject("error")
			}
		})
	})
}

export function getWithParam(url, param) {
	let token = uni.getStorageSync("token")
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			header: {
				'token': token
			},
			data: param,
			success: (res) => {
				if (res.data.code == 2000) {
					resolve(res.data)
				} else if (res.data.code == 3000) {
					uni.navigateTo({
						url: "/pages/login/index"
					})
				} else {
					uni.showToast({
						icon: "none",
						title: res.data.message
					})
				}

			},
			fail: () => {
				reject("error")
			}
		})
	})
}


export function getWithParam_(url, param) {
	uni.showLoading({
		title: "加载中"
	})
	let token = uni.getStorageSync("token")
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			header: {
				'token': token
			},
			data: param,
			success: (res) => {
				uni.hideLoading()
				if (res.data.code == 2000) {
					resolve(res.data)
				} else if (res.data.code == 3000) {
					uni.navigateTo({
						url: "/pages/login/index"
					})
				} else {
					uni.showToast({
						icon: "none",
						title: res.data.message
					})
				}

			},
			fail: () => {
				uni.hideLoading()
				uni.hideLoading()
				uni.showToast({
					icon: "none",
					title: "网络错误"
				})
				reject("error")
			}
		})
	})
}

export function postJson(url, params) {
	let token = uni.getStorageSync("token")
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			header: {
				'token': token
			},
			method: "POST",
			data: params,
			success: (res) => {
				if (res.data.code == 2000) {
					resolve(res.data)
				} else if (res.data.code == 3000) {
					uni.navigateTo({
						url: "/pages/login/index"
					})
				} else {
					uni.showToast({
						icon: "none",
						title: res.data.message
					})
				}

			},
			fail: () => {
				reject("error")
			}
		})
	})
}

export function postWithParam(url, param) {
	let token = uni.getStorageSync("token")
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			header: {
				'token': token,
				'content-type': 'application/x-www-form-urlencoded'
			},
			method: "POST",
			data: param,
			success: (res) => {
				if (res.data.code == 2000) {
					resolve(res.data)
				} else if (res.data.code == 3000) {
					uni.navigateTo({
						url: "/pages/login/index"
					})
				} else {
					uni.showToast({
						icon: "none",
						title: res.data.message
					})
				}

			},
			fail: () => {
				reject("error")
			}
		})
	})
}


export function postWithParam_(url, param) {
	uni.showLoading({
		title: "加载中"
	})
	let token = uni.getStorageSync("token")
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			header: {
				'token': token,
				'content-type': 'application/x-www-form-urlencoded'
			},
			method: "POST",
			data: param,
			success: (res) => {
				uni.hideLoading()
				if (res.data.code == 2000) {
					resolve(res.data)
				} else if (res.data.code == 3000) {
					uni.navigateTo({
						url: "/pages/login/index"
					})
				} else {
					uni.showToast({
						icon: "none",
						title: res.data.message
					})
				}

			},
			fail: () => {
				uni.hideLoading()
				uni.hideLoading()
				uni.showToast({
					icon: "none",
					title: "网络错误"
				})
				reject("error")
			}
		})
	})
}
